package cn.iocoder.yudao.module.forward.service.datasource;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.forward.controller.admin.datasource.vo.DataSourcePageReqVO;
import cn.iocoder.yudao.module.forward.controller.admin.datasource.vo.DataSourceSaveReqVO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasource.DataSourceDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasourceinfo.DataSourceInfoDO;
import cn.iocoder.yudao.module.forward.dal.mysql.datasource.DataSourceMapper;
import cn.iocoder.yudao.module.forward.mqtt.MqttConnect;
import cn.iocoder.yudao.module.forward.mqtt.MqttInfoCreateUtil;
import cn.iocoder.yudao.module.forward.mqtt.MqttManager;
import cn.iocoder.yudao.module.forward.service.datasourceinfo.DataSourceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.DATA_SOURCE_NOT_EXISTS;

/**
 * 信息源 Service 实现类
 *
 * @author dfs
 */
@Slf4j
@Service
@Validated
public class DataSourceServiceImpl implements DataSourceService {
    @Resource(name = "SourceMqttManager")
    private MqttManager mqttManager;
    @Resource
    private DataSourceMapper dataSourceMapper;
    @Resource
    private DataSourceInfoService dataSourceInfoService;

    @Override
    public Integer createDataSource(DataSourceSaveReqVO createReqVO) {
        // 插入
        DataSourceDO dataSource = BeanUtils.toBean(createReqVO, DataSourceDO.class);
        dataSource.setStatus(false);
        dataSourceMapper.insert(dataSource);
        // 返回
        return dataSource.getId();
    }

    @Override
    public void updateDataSource(DataSourceSaveReqVO updateReqVO) {
        // 校验存在
        validateDataSourceExists(updateReqVO.getId());
        // 更新
        DataSourceDO updateObj = BeanUtils.toBean(updateReqVO, DataSourceDO.class);
        dataSourceMapper.updateById(updateObj);
        if (!updateReqVO.getEnable()) {
            mqttManager.closeMqttConnect(updateObj.getName());
        } else {
            DataSourceInfoDO info = dataSourceInfoService.getDataSourceInfoBySourceId(updateObj.getId());
            MqttInfoCreateUtil.tryDataSourceConnect(mqttManager, updateObj, info);
        }
    }

    @Override
    public void deleteDataSource(Integer id) {
        // 校验存在
        validateDataSourceExists(id);
        DataSourceDO dataSource = getDataSource(id);
        // 删除
        dataSourceMapper.deleteById(id);
        mqttManager.closeMqttConnect(dataSource.getName());
    }

    private void validateDataSourceExists(Integer id) {
        if (dataSourceMapper.selectById(id) == null) {
            throw exception(DATA_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public DataSourceDO getDataSource(Integer id) {
        return dataSourceMapper.selectById(id);
    }

    @Override
    public PageResult<DataSourceDO> getDataSourcePage(DataSourcePageReqVO pageReqVO) {
        PageResult<DataSourceDO> dataSourceDOPageResult = dataSourceMapper.selectPage(pageReqVO);
        List<DataSourceDO> list = dataSourceDOPageResult.getList();
        for (DataSourceDO dataSourceDO : list) {
//            try {
//                BrokerDO broker = brokerService.getBroker(dataSourceDO.getBrokerId());
//                dataSourceDO.setClientName(broker.getClientName());
//            }catch (Exception e){
//                e.printStackTrace();
//            }
            MqttConnect mqttConnect = mqttManager.getMqttConnect(dataSourceDO.getName());
            dataSourceDO.setStatus(mqttConnect != null && mqttConnect.isConnected());
        }
        return dataSourceDOPageResult;
    }
}